<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/8/24
 * Time: 17:31
 */

namespace Biz\module\Classes;

use Biz\model\trade\OrderPackage as mOrderPackage;
use Biz\service\SkuInfo as sSkuInfo;
use Biz\service\trade\Logistics as sLogistics;
use Biz\service\trade\ShopCourier as sShopCourier;
use Biz\model\trade\Order as mOrder;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\module\Classes\OrderPackage as clsOrderPackage;


class OrderGoods
{
    const SKU_IS_FAIL = -1;
    const SKU_IS_NOT_SHIP = 1;
    const SKU_IS_SHIP = 2;

    //判断sku是否已经发货，包括部分发货的情况
    public static function checkSkuIsDeliveryIncludePartShip($orderNo, $skuId, $orderInfo)
    {
        //  -1异常  1未发货  2已发货
        $skuIsShip = self::SKU_IS_NOT_SHIP;

        if (in_array($orderInfo['status'], [mOrder::STATUS_NORMAL, mOrder::STATUS_CLOSE, mOrder::STATUS_DEL])) {
            return self::SKU_IS_FAIL;
        }

        //未发货则初始化数据
        if ($orderInfo['is_package_delivery'] == mOrder::IS_PACKAGE_DELIVERY_NO) {
            if ($orderInfo['status'] != mOrder::STATUS_WAIT_SEND) {
                $skuIsShip = self::SKU_IS_SHIP;
            }

        } elseif ($orderInfo['is_package_delivery'] == mOrder::IS_PACKAGE_DELIVERY_YES) {
            $check = mOrderPackage::checkSkuIdsDelivery($orderNo, [$skuId]);

            if ($check) {
                $skuIsShip = self::SKU_IS_SHIP;
            }
        }

        return $skuIsShip;

    }

    //根据orderno和skuid，获取发货的快递编号
    public static function getExpressInfo($orderNo, $skuIds = [])
    {
        $orderInfo = mOrder::getOrderInfo($orderNo);

        if (empty($skuIds)) {
            $skuIds = mOrderGoods::getSkuIds($orderNo);
        }

        $packageList = clsOrderPackage::getOrderPackageList($orderNo, true);

        if (empty($packageList)) {
            return [];
        }

        $skuPackage = [];

        array_map(function ($v) use (&$skuPackage) {
            foreach ($v['sku_list'] as $info) {
                $skuPackage[$info['sku_id']] = [
                    'express_company' => $v['express_company'],
                    'express_no' => $v['express_no']
                ];
            }
        }, $packageList);

        return $skuPackage;


    }


}